package com.market.inventory.server.controller;
import com.github.pagehelper.PageInfo;
import com.market.common.core.constant.HttpStatus;
import com.market.common.core.domain.Result;
import com.market.common.core.web.page.TableDataInfo;
import com.market.common.security.utils.SecurityUtils;
import com.market.inventory.server.City;
import com.market.inventory.server.purchase.SamPurchase;
import com.market.inventory.server.service.SamInventoryService;
import com.market.inventory.server.vo.VoProductSku;
import com.market.inventory.server.vo.VoPurchase;
import com.market.product.common.domain.ProductInfo;
import com.market.product.common.domain.ProductSkuInfo;
import com.market.system.common.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

import static com.market.common.core.utils.PageUtils.startPage;

@RestController
@RequestMapping("/inventory")
public class SamInventoryController {
    @Autowired
    SamInventoryService samInventoryService;

    /**
     * 预警列表
     * @return
     */
    @PostMapping("/forewarningList")
    private Result<TableDataInfo> forewarninglist(){
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<ProductInfo> list = samInventoryService.forewarninglist();
        return getDataTable(list);
    }


    protected Result<TableDataInfo> getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        Result<TableDataInfo> returnResult = new Result<>();

        returnResult.setCode(HttpStatus.SUCCESS);
        returnResult.setMsg("查询成功");
        returnResult.setData(rspData);

        return returnResult;
    }
    /**
     * 预警列表详情
     * @return
     */
    @PostMapping("/forewarningdetails/{id}")
    private Result<TableDataInfo> forewarningdetails(@PathVariable Integer id){
        startPage();
        List<ProductSkuInfo> skuInfos=samInventoryService.forewarningdetails(id);
        return getDataTable(skuInfos);
    }
    /**
     * 生成采购单
     */
    @PostMapping("/purchaseInsert")
    private Result purchase(@RequestBody SamPurchase samPurchase){
        Integer i=samInventoryService.purchase(samPurchase);
        return Result.success("成功生成采购单");
    }
    /**
     * 查看采购单
     */
    @PostMapping("/purchaseList")
    private Result purchaseList(){
        startPage();
        List<SamPurchase> samPurchases=samInventoryService.purchaseList();
        return getDataTable(samPurchases);
    }
    /**
     * 审批
     */
    @PostMapping("/updateStatus")
    private Result updateStatus(@RequestBody VoPurchase voPurchase){
        startPage();
        Integer i=samInventoryService.updateStatus(voPurchase);
        return Result.success(i);
    }
    /**
     * 采购商品
     */
    @PostMapping("/purchaseUpdate")
    private Result purchaseUpdate(@RequestBody VoProductSku voProductSku){
        startPage();
        Integer i=samInventoryService.purchaseUpdate(voProductSku);
        return Result.success(i);
    }
    /**
     * 三级两动
     */
    @PostMapping("/city/{cityId}")
    private Result<List<City>> city(@PathVariable Integer cityId){
        return samInventoryService.city(cityId);
    }




}
